/**
 * Copyright (c) 2010 Basil Shikin, BugStat Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.bugstat.gui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bugstat.gui.wizard.BugstartReportWizardModel;
import org.jdesktop.swingx.JXBusyLabel;
import org.pietschy.wizard.Wizard;
import org.pietschy.wizard.WizardEvent;
import org.pietschy.wizard.WizardListener;

/**
 * This is main frame for bugstat import wizard application. 
 *
 * @author Basil Shikin
 */
public class BugstatFrame
    extends JFrame
{
    /**
     * 
     */
    private static final String BUGSTAT_MAIN_WINDOW = "bugstat.gui.main_frame";

    private static Log log = LogFactory.getLog( BugstatFrame.class );
    
    private static final long serialVersionUID = 1L;

    private static final String PAGE_BUSY   = "page_busy";
    private static final String PAGE_WIZARD = "page_wizard";

    private JXBusyLabel busySpinner;    
    private JPanel contentPanel;    
    
    public void initializeFrame()
    {
        setTitle( UIStrings.TEXT_BUGSTAT_REPORT_GENERATOR );

        busySpinner = new JXBusyLabel( new Dimension( 24, 24 ));
        busySpinner.setText( UIStrings.TEXT_LOADING+ "..." );
        busySpinner.setBusy( true );
        
        // Create a panel for it
        final JPanel busyPanel = new JPanel( new MigLayout("fill", "0[center]0", "15%[]push"));
        busyPanel.setOpaque( false );
        busyPanel.add( busySpinner );
        
        contentPanel = new JPanel( new CardLayout() );
        contentPanel.add( busyPanel, PAGE_BUSY );
        
        setLayout( new BorderLayout() );
        add( contentPanel, BorderLayout.CENTER );
        
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
        switchToPage( PAGE_BUSY );
        
        // Start a thread that initialized the wizard
        startWizardIntialization();
        
        // 
        restoreLocation();
        
    }
    
    private void restoreLocation()
    {
        PropertyTracker tracker = new PropertyTracker();
        
        // Load window location
        final Dimension defaultSize = new Dimension( 500, 800 );
        final Dimension size = tracker.get( BUGSTAT_MAIN_WINDOW, defaultSize);
        
        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        final Point defaultLocation = new Point( screenSize.width/2 - (size.width/2), screenSize.height/2 - (size.height/2) );
        final Point location = tracker.get( BUGSTAT_MAIN_WINDOW, defaultLocation);
        
        setBounds( new Rectangle( location, size) );
        setPreferredSize( size );
        
        // Track window
        tracker.trackWindow( this, BUGSTAT_MAIN_WINDOW);        
    }

    private void initializeWizard() throws Exception
    {
        final BugstartReportWizardModel wizardModel = new BugstartReportWizardModel();
        wizardModel.initialize();
        
        final Wizard wizard = new Wizard( wizardModel );
        wizard.setOverviewVisible( false );        
        wizard.setDefaultExitMode( Wizard.EXIT_ON_FINISH );
                
        wizard.getPreviousAction().putValue(Action.SMALL_ICON, null );
        wizard.getPreviousAction().putValue(Action.NAME, "< " + UIStrings.TEXT_BACK );
        wizard.getNextAction().putValue(Action.NAME, UIStrings.TEXT_NEXT + " >");
        wizard.getNextAction().putValue(Action.SMALL_ICON, null );
        
        wizard.addWizardListener( new WizardListener() {
            public void wizardClosed(WizardEvent e)
            {
                System.exit( 0 );
            }
            
            public void wizardCancelled(WizardEvent e)
            {
                System.exit( -1 );
            }
        } );
        
        final JPanel wizardPanel = new JPanel( new BorderLayout() );
        wizardPanel.add( wizard, BorderLayout.CENTER );
        
        // Show wizard page (do it in UI thread)
        SwingUtilities.invokeLater( new Runnable() {
            @Override
            public void run()
            {
                contentPanel.add( wizardPanel, PAGE_WIZARD );
                switchToPage( PAGE_WIZARD );
                                
                busySpinner.setBusy( false );
            }
        } );
    }
    
    private void startWizardIntialization()
    {
        new Thread("initialize_wizard") {
            @Override
            public void run()
            {
                try
                {
                    initializeWizard();
                }
                catch ( Throwable e )
                {
                    log.error("Unable to start bug export wizard", e );
                }
            }
        }.start();
    }
    
    private void switchToPage( final String pageName )
    {
        final CardLayout cardLayout = (CardLayout)contentPanel.getLayout();
        cardLayout.show( contentPanel, pageName);
    }
}